//
//  CXHServiceAdViewController.m
//  CXHAdSDK_Example
//
//  Created by 陈坤 on 2023/1/4.
//  Copyright © 2023 华资融创. All rights reserved.
//

#import "CXHServiceAdViewController.h"
#import <CXHAdSDK-Core/CXHAdSDK.h>
#import <CXHAdSDK-Core/CXHAdSDKServiceAdView.h>
#import <CXHAdSDK-Kit/CXHUtilsKit.h>

@interface CXHServiceAdViewController ()<CXHSDKServiceAdViewDelegate>

@property (nonatomic, strong) CXHAdSDKServiceAdView *serviceAdView;

@end

@implementation CXHServiceAdViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIBarButtonItem *itemLeft = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(onCancelClicked:)];
    self.navigationItem.leftBarButtonItem = itemLeft;
    
    UIButton *btn2 = [UIButton new];
    btn2.backgroundColor = [UIColor orangeColor];
    btn2.layer.cornerRadius = 20;
    
    btn2.frame = CGRectMake((kCXHScreenWidth -150)/2, kCXHTopBarHeight + 50, 150, 40);
    [btn2 setTitle:@"load banner" forState:UIControlStateNormal];
    [btn2 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.view addSubview:btn2];
    [btn2 addTarget:self action:@selector(test2) forControlEvents:UIControlEventTouchUpInside];
}

- (void)onCancelClicked:(id)sender {
    [self.navigationController dismissViewControllerAnimated:YES completion:nil];
}


- (void)test2 {
    if (_serviceAdView) {
        [_serviceAdView removeFromSuperview];
        _serviceAdView = nil;
    }
    
    CGFloat height = [UIScreen mainScreen].bounds.size.width * 110/335;
    CGFloat width = [UIScreen mainScreen].bounds.size.width;
    
    // 1 初始化banner视图
    
    _serviceAdView = [[CXHAdSDKServiceAdView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
    _serviceAdView.delegate = self;
    _serviceAdView.controller = self;
    _serviceAdView.refershTime = 35;
    _serviceAdView.posId = @"50164389";
    _serviceAdView.backgroundColor = [UIColor whiteColor];
    
    // 2 添加到父视图上
    _serviceAdView.frame = CGRectMake(50, [UIScreen mainScreen].bounds.size.height  - 50 - 100, 50, 50);
    [self.view addSubview:_serviceAdView];
    
    // 3 加载并显示广告 注意: 请确保banner视图显示在屏幕内的时候,调用load方法
    [_serviceAdView loadAndShow];
}


#pragma mark - CXHSDKServiceAdViewDelegate

/**
 广告获取成功
 
 @param serviceAdView banner实例
 */
- (void)CXH_serviceAdViewDidReceived:(CXHAdSDKServiceAdView *)serviceAdView{
    
}

/**
 广告拉取失败
 
 @param serviceAdView banner实例
 @param error 错误描述
 */
- (void)CXH_serviceAdViewFailToReceived:(CXHAdSDKServiceAdView *)serviceAdView error:(NSError *)error{
    [_serviceAdView removeFromSuperview];
    _serviceAdView = nil;
}

/**
 广告点击
 
 @param serviceAdView 广告实例
 @param loadingPageURL 广告落地页地址，当渠道为bwt，并且customLoadingPage为YES时有值
 */
- (void)CXH_serviceAdViewClicked:(CXHAdSDKServiceAdView *)serviceAdView loadingPageURL:(NSString *)loadingPageURL{
    
}

/**
 广告关闭
 
 @param serviceAdView 广告实例
 */
- (void)CXH_serviceAdViewClose:(CXHAdSDKServiceAdView *)serviceAdView{
    dispatch_async(dispatch_get_main_queue(), ^{
        _serviceAdView = nil;
    });
}

/**
 广告展示
 
 @param serviceAdView 广告实例
 */
- (void)CXH_serviceAdViewExposure:(CXHAdSDKServiceAdView *)serviceAdView{
    
}

/**
 关闭落地页
 
 @param serviceAdView 广告实例
 */
- (void)CXH_serviceAdViewCloseLandingPage:(CXHAdSDKServiceAdView *)serviceAdView{
    
}

- (void)CXH_serviceAdViewClickedReport:(nonnull CXHAdSDKServiceAdView *)serviceAdView { 
    
}


- (void)CXH_serviceAdViewExposureReport:(nonnull CXHAdSDKServiceAdView *)serviceAdView { 
    
}
@end
